package sise.world.map;

import java.awt.Point;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.swing.Icon;

import sise.world.agent.AbstractAgent;
import sise.world.utils.ImageHolder;

public class Field {

	private FieldType fieldType;
	private Point coordinates;
	private AbstractAgent presentAgent;

	private int resources;

	public enum FieldType {
		GROUND("/img/pusty.jpg"),
		WORM("/img/larwa.jpg"),
		TREE("/img/drzewo.jpg"),
		FOOD("/img/owocek.jpg"),  //chwilowo ikona jedzenia (roslinki) to otwór
		MOUNTAIN("/img/kamien.jpg");

		private String iconFilePath;

		private FieldType(String iconFilePath) {
			this.iconFilePath = iconFilePath;
		}

		public String getIconFilePath() {
			return iconFilePath;
		}

		public void setIconFilePath(String iconFilePath) {
			this.iconFilePath = iconFilePath;
		}

	}

	public Field() {
		super();
	}

	public Field(Point coordinates) {
		super();
		this.coordinates = coordinates;
	}

	public Field(FieldType fieldType, Point coordinates) {
		super();
		this.fieldType = fieldType;
		this.coordinates = coordinates;
	}

	public Field(FieldType fieldType, Point coordinates, AbstractAgent presentAgent) {
		super();
		this.fieldType = fieldType;
		this.coordinates = coordinates;
		this.presentAgent = presentAgent;
	}

	public Icon getIcon() throws IOException, URISyntaxException {
		return ImageHolder.getInstance().getIcon(getFieldType(), getPresentAgent());
	}

	public String getToolTip() {
		StringBuilder toolTipBuilder = new StringBuilder();
		toolTipBuilder.append("<html>");
		toolTipBuilder.append("Field type: <b>");
		toolTipBuilder.append(fieldType.name());
		toolTipBuilder.append("</b>");
		toolTipBuilder.append("<br />");
		toolTipBuilder.append("Resources: <b>");
		toolTipBuilder.append(resources);
		toolTipBuilder.append("</b>");
		if (presentAgent != null) {
			toolTipBuilder.append("<br /><hr />");
			toolTipBuilder.append(presentAgent.getToolTip());
		}
		toolTipBuilder.append("</html>");
		return toolTipBuilder.toString();

	}

	public FieldType getFieldType() {
		return fieldType;
	}

	public void setFieldType(FieldType fieldType) {
		this.fieldType = fieldType;
	}

	public Point getCoordinates() {
		return coordinates;
	}

	public void setCoordinates(Point coordinates) {
		this.coordinates = coordinates;
	}

	public AbstractAgent getPresentAgent() {
		return presentAgent;
	}

	public void setPresentAgent(AbstractAgent presentAgent) {
		this.presentAgent = presentAgent;
	}

	public int getResources() {
		return resources;
	}

	public void setResources(int resources) {
		this.resources = resources;
	}
	
	public void modifyResources(int delta){
		if(!this.fieldType.equals(FieldType.GROUND)){
			if(resources < Integer.MAX_VALUE - delta){
				resources += delta;
				if(resources <= 0){
					this.fieldType = FieldType.GROUND;
				}
			}
		}
	}
	
	public String toString(){
		return this.fieldType.toString();
	}

}
